const db = wx.cloud.database();
// 添加
function add(collection = "", data = {}) {
  return db.collection(collection).add({data: data})
}
// 删除
function remove(collection = "", id = "") {
  return db.collection(collection).doc(id).remove()
}
// 更新、修改
function update(collection = "", id = "", data = {}) {
  return db.collection(collection).doc(id).update({data: data})
}
// 批量更新、修改
function multiUpdate(collection = "", data = {}, data1 = {}) {
    return db.collection(collection).where(data).update({data: data1})
}
// 查询
function get(_collection = "", _where = {}) {
  return db.collection(_collection).where(_where).get()
}
function getById(_collection = "", id="") {
  return db.collection(_collection).doc(id).get()
}
// 模糊查询
function dimGet(_collection = "", name) {
  return db.collection(_collection).where({
    name: db.RegExp({
      regexp: name,
    })
  }).get()
}
// views累加
function viewsInc(_collection="",id="",count=1) {
  const _ = db.command
  return db.collection(_collection).doc(id).update({
    data: {
      views: _.inc(count)
    }
  })
}
// likes累加
function likesInc(_collection="",id="",count=1) {
  const _ = db.command
  return db.collection(_collection).doc(id).update({
    data: {
      likes: _.inc(count)
    }
  })
}
// likes累减
function likesSub(_collection="",id="",count=-1) {
  const _ = db.command
  return db.collection(_collection).doc(id).update({
    data: {
      likes: _.inc(count)
    }
  })
}
// 查询条数
function count(_collection="",where={}){
  return db.collection(_collection).where(where).count()
}
// 批量删除
function del(_collection="",where={}){
  return wx.cloud.callFunction({
    name:"del",
    data:{
      collection:_collection,
      where:where
    }
  })
}
// 指定值查询
function getLikes(_collection="",arr=[]){
  const _ = db.command
  return db.collection(_collection).where({
    _id: _.in(arr)
  }).get()
}
export {
  add,
  remove,
  update,
  multiUpdate,
  get,
  getById,
  dimGet,
  viewsInc,
  likesInc,
  likesSub,
  count,
  del,
  getLikes
}